package cn.stu.framework.service.impl;

import cn.stu.common.utils.ApiData;
import cn.stu.framework.dto.LoginDto;
import cn.stu.framework.entity.LoginLog;
import cn.stu.framework.mapper.LoginLogMapper;
import cn.stu.framework.service.LoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录日志服务实施
 *
 * @author 郑志豪
 * @date 2023/09/12
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {
    /**
     * 登录日志映射器
     */
    @Autowired
    private LoginLogMapper loginLogMapper;

    /**
     * 添加日志
     *
     * @param request 请求
     * @param users   用户
     * @param res     事由
     */
    @Override
    public void addLog(HttpServletRequest request, LoginDto users, ApiData res) {
        LoginLog log = new LoginLog();
        log.setAcc(users.getAcc());
        log.setIpaddr(request.getRemoteHost());
        log.setMsg(res.getMsg() + (res.getCode() == 0 ? "" : "用户尝试密码：" + users.getPwd()));
        log.setStatus(res.getCode());
        loginLogMapper.insert(log);
    }
}
